package tornadoj.web;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

/**
 * Created by tinyms on 13-12-25.
 */
public class TornadoJConfiguration {
    public final static String Ver = "1.1";
    private static boolean isLoaded = false;

    public static Properties Items = new Properties();

    public static String SiteUrl = "http://localhost:8080";
    /**
     * WebApp absolute path
     */
    public static String SiteAbsPath = "/";
    /**
     * template path
     */
    public static String TemplatePath = "/WEB-INF/templates/";
    /**
     * Lucene index store root path
     */
    public static String LuceneIndexerPath = "d:/lucene-index/";
    /**
     * plugin package name for search
     */
    public static Set<String> PluginPackagePrefix = new HashSet<String>();
    /**
     * File upload cache size, default 1M
     */
    public static int FileUploadSizeTempCache = 1024 * 1024;
    /**
     * File upload limit size, default 5M
     */
    public static int FileUploadSizeLimit = 5 * 1024 * 1024;
    /**
     * Date formatter
     */
    public static String DateFormat = "yyyy-MM-dd";
    public static String DateTimeFormat = "yyyy-MM-dd hh:mm:ss";

    public static void loadProperties() {
        if(!isLoaded){
            InputStream io = TornadoJConfiguration.class.getResourceAsStream("/tornadoj.properties");
            Properties props = TornadoJConfiguration.Items;
            try {
                props.load(io);
                io.close();
                TornadoJConfiguration.SiteUrl = props.getProperty("app.site", "http://localhost:8080");
                String pluginPkgPrefixStr = props.getProperty("app.packages", "");
                if (StringUtils.isNotBlank(pluginPkgPrefixStr)) {
                    String[] pkgs = pluginPkgPrefixStr.split(";");
                    TornadoJConfiguration.PluginPackagePrefix.clear();
                    for (String pkg : pkgs) {
                        TornadoJConfiguration.PluginPackagePrefix.add(pkg);
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            isLoaded = true;
        }
    }

}
